from public import settins
from apps.snmp import *
from requests.exceptions import RequestException
from simplejson.errors import JSONDecodeError

api = settins.Api()



def start(item):
    try:
        d = scan_index_snmp(hostname=item['host'], oids_dict=IP_OID)
        res = settins.Handel_Dict_Data(d)
        r = settins.Response(hostname=item['hostname'],host=item['host'],application="snmp获取ip接口",data=res).Success
        api.post(url="/devops/device/ip/",data=r)
    except EasySNMPError as es:
        r = settins.Response(hostname=item['hostname'],host=item['host'],application="snmp获取ip接口",data="snmp出错!").Error
        api.error(data=r)
    except RequestException as re:
        r = settins.Response(hostname=item['hostname'],host=item['host'],application="snmp获取ip接口",data="api请求错误！").Error
        api.error(data=r)
    except JSONDecodeError as je:
        r = settins.Response(hostname=item['hostname'],host=item['host'],application="snmp获取ip接口",data="json解析失败").Error
        api.error(data=r)
    except Exception as e:
        print(e)
        r = settins.Response(hostname=item['hostname'],host=item['host'],application="snmp获取ip接口",data=str(e)).Error
        api.error(data=r)

def main():
    device_list = api.get_device
    settins.Start_Thread(start,device_list)



if __name__ == '__main__':
    main()